package com.blb.http;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 自定义Http服务器
 */
public class MyHttpServer {

    public static final int PORT = 999;
    public static final String HTML_DIR = "D:\\home";

    /**
     * 读取流中的文字
     */
    private String read(InputStream inputStream){
        StringBuilder builder = new StringBuilder();
        try{
            //创建字符流
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            //循环读取一行文字
            while((line = reader.readLine()) != null){
                builder.append(line + "\n");
                if (line.length() == 0) {//""
                    break;
                }
            }
        }catch (IOException ex){
            ex.printStackTrace();
        }
        return builder.toString();
    }

    public void start(){
        System.out.println("启动服务器");
        //创建ServerSocket
        try(ServerSocket serverSocket = new ServerSocket(PORT)){
            while(true){
                //获得客户端连接
                Socket socket = serverSocket.accept();
                System.out.println("客户端连接：" + socket);
                //读取客户端的请求报文
//                StringBuilder request = new StringBuilder();
////                //读取请求报文
////                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
////                String line = null;
////                while ((line = bufferedReader.readLine()) != null) {
////                    request.append(line);
////                    if (line.length() == 0) {//""
////                        break;
////                    }
////                }
                String request = read(socket.getInputStream());
                System.out.println(request);
                //截取报文中的url
                int first = request.indexOf(" ");
                int second = request.indexOf(" ",first + 1);
                String url = request.substring(first + 1, second);
                System.out.println("url:" + url);
                File file = new File(HTML_DIR + url);
                StringBuilder stringBuilder = new StringBuilder();
                if(file.exists()){
                    //读取本地html文件
                    StringBuilder html = new StringBuilder();
                    try(BufferedReader reader = new BufferedReader(new FileReader(file))){
                        String s = null;
                        while((s = reader.readLine()) != null){
                            html.append(s + "\n");
                        }
                    }catch (IOException ex){
                        ex.printStackTrace();
                    }
//                    String html = read(new FileInputStream(file));
                    System.out.println("html:" + html);
                    //拼接响应报文
                    stringBuilder.append("HTTP/1.1 200 OK\r\n");
                    stringBuilder.append("Content-Type: text/html; charset=UTF-8\r\n");
                    stringBuilder.append("Content-Length: " + html.toString().getBytes("UTF-8").length + "\r\n");
                    stringBuilder.append("\r\n");
                    stringBuilder.append(html);
                }else{
                    System.out.println("文件不存在" + file.getAbsolutePath());
                    stringBuilder.append("HTTP/1.1 404 NOT FOUND\r\n");
                }
                //发送响应报文给浏览器
                OutputStream outputStream = socket.getOutputStream();
                outputStream.write(stringBuilder.toString().getBytes());
                outputStream.flush();
                outputStream.close();
            }
        }catch (IOException ex){
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new MyHttpServer().start();
    }
}
